<?php
/**
 * 考场
 */
namespace app\modules\content\controllers;

use app\libs\ApiControl;
use app\libs\GetData;
use app\libs\Pager;
use yii;
use app\libs\IeltsController;
use app\modules\content\models\Examroom;
use app\modules\content\models\ExamroomImage;

class ExamroomController extends  ApiControl
{
    public $enableCsrfValidation = false;


    public function actionIndex()
    {
        $page=Yii::$app->request->get('page',1);
        $city=Yii::$app->request->get('city','');
        $area=Yii::$app->request->get('area','');
        $offset=($page-1)*15;
        $where='1=1';
        if($city!=false){
            $where.=' and city="'.$city.'"';
        }
        if($area!=false){
            $where.=' and area='.$area;
        }
        $data = Examroom::find()->distinct()->asArray()->where($where)->offset($offset)->limit(15)->all();
        $city = Examroom::find()->distinct()->asArray()->select('city')->all();
        $count = Examroom::find()->distinct()->asArray()->where($where)->count();

        $url = strstr($_SERVER['REQUEST_URI'], '?') == false ? $_SERVER['REQUEST_URI'] . '?page' : $_SERVER['REQUEST_URI'] . '&page';
        $count = $count;
        $page = new Pager("$url", $count, $page, 15);
        $str = $page->GetPager();
        return $this->render('index', ['data'=>$data,'city'=>$city,'str'=>$str]);
    }

    public function actionAdd()
    {
        if (!$_POST) {
            $id = Yii::$app->request->get('id', '');
            if (empty($id)) {
                return $this->render('add');
            } else {
                $data = Yii::$app->db->createCommand("select * from {{%examination}} where id=" . $id)->queryOne();
                return $this->render('add', ['data' => $data]);
            }

        } else {
            $model = new Examroom();
            $data = Yii::$app->request->post();
            $getdata = new GetData();
            $must = array('name' => '考场名','city'=>'城市');
            $data = $getdata->PostData($must, 'examination');
            $data['createTime'] = time();
            if (empty($data['id'])) {
                $re = Yii::$app->db->createCommand()->insert("{{%examination}}", $data)->execute();
            } else {
                $re = $model->updateAll($data, 'id=:id', array(':id' => $data['id']));
            }
            if ($re) {
                $this->redirect('index');
            } else {
                echo '<script>alert("数据修改/添加失败，请重试");history.go(-1);</script>';
                die;
            }
        }

    }

    /**
     * [actionImage 考点城市背景图]
     * @Author don
     * @Date   2021-10-28
     * @return [type]     [description]
     */
    public function actionImage()
    {
        $page = Yii::$app->request->get('page',1);
        $city = Yii::$app->request->get('city','');
        $offset = ($page-1) * 15;
        $where = '1=1';
        if ($city != false) {
            $where.=' and city="'.$city.'"';
        }
        $data = ExamroomImage::find()->distinct()->asArray()->where($where)->offset($offset)->limit(15)->all();
        $city = ExamroomImage::find()->distinct()->asArray()->select('city')->all();
        $count = ExamroomImage::find()->distinct()->asArray()->where($where)->count();

        $url = strstr($_SERVER['REQUEST_URI'], '?') == false ? $_SERVER['REQUEST_URI'] . '?page' : $_SERVER['REQUEST_URI'] . '&page';
        $count = $count;
        $page = new Pager("$url", $count, $page, 15);
        $str = $page->GetPager();
        return $this->render('image', ['data'=>$data,'city'=>$city,'str'=>$str]);
    }

    /**
     * [actionAddImage 编辑图片]
     * @Author don
     * @Date   2021-10-28
     * @return [type]     [description]
     */
    public function actionAddImage()
    {
        if (!$_POST) {
            $id = Yii::$app->request->get('id', '');
            if (empty($id)) {
                return $this->render('add_image');
            } else {
                $data = Yii::$app->db->createCommand("select * from {{%examination_image}} where id=" . $id)->queryOne();
                return $this->render('add_image', ['data' => $data]);
            }

        } else {
            $model = new ExamroomImage();
            $data = Yii::$app->request->post();
            $getdata = new GetData();
            $must = array('pic' => '背景图片','city'=>'城市');
            $data = $getdata->PostData($must, 'examinationImage');
            $data['createTime'] = time();
            if (empty($data['id'])) {
                $re = Yii::$app->db->createCommand()->insert("{{%examination_image}}", $data)->execute();
            } else {
                $re = $model->updateAll($data, 'id=:id', array(':id' => $data['id']));
            }
            if ($re) {
                $this->redirect('image');
            } else {
                echo '<script>alert("数据修改/添加失败，请重试");history.go(-1);</script>';
                die;
            }
        }

    }

    /**
     * [actionDel 删除背景图]
     * @Author don
     * @Date   2021-10-28
     * @return [type]     [description]
     */
    public function actionDelImage()
    {
        $id = Yii::$app->request->get('id', '');
        $re = ExamroomImage::deleteAll("id=:id", array(':id' => $id));
        if ($re) {
            echo true;
        }

    }
}